package cn.daomeng.firebird.core.utils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * HTML工具类
 * @author Qin.SiLiang
 * @since 2014/11/19
 */
@SuppressWarnings("unused")
public class HttpUtils extends HttpUtil{


	/**
	 * http post方式发送消息
	 */
	public static String doGet(String url, Integer timeout ,String charset ) {

		HttpRequest doGet = HttpUtil.createGet(url);
		if(timeout == null) {
			doGet.timeout(Constants.HTTP_TIME_OUT_20SECOND);
		}else {
			doGet.timeout(timeout);
		}
		if(StringUtils.isBlank(charset)) {
			doGet.charset(CharsetUtil.UTF_8);
		}else {
			doGet.charset(charset);
		}
		HttpResponse execute = doGet.execute();
		if(Constants.HTTP_SUCCESS_CODE == execute.getStatus()) {
			return  execute.body();
		}else {
			return null;
		}
	}

	/**
	 * http post方式发送消息
	 * @author Qin.Siliang
	 * @since 2019/3/4
	 * @param url 地址
	 * @param parameters map参数
	 * @param timeout 超时时间
	 * @param charset 编码格式（默认UTF-8）
	 * @return java.lang.String
	 * @throws
	 */
	public static String doPost(String url, Map<String, Object> parameters, Integer timeout ,String charset ) {

		HttpRequest postRequest = HttpUtil.createPost(url);
		if(timeout == null) {
			postRequest.timeout(Constants.HTTP_TIME_OUT_20SECOND);
		}else {
			postRequest.timeout(timeout);
		}
		if(StringUtils.isBlank(charset)) {
			postRequest.charset(CharsetUtil.UTF_8);
		}else {
			postRequest.charset(charset);
		}
		if(parameters != null) {
			postRequest.form(parameters);
		}
		HttpResponse execute = postRequest.execute();
		if(Constants.HTTP_SUCCESS_CODE == execute.getStatus()) {
			return  execute.body();
		}else {
			return null;
		}
	}

}
